package com.heima.schedule.controller;

import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.schedule.dtos.Task;
import com.heima.schedule.service.TaskService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ap1/v1/task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    /**
     * 添加定时任务
     * @param task
     * @return
     */
    @PostMapping("/add")
    public ResponseResult addTask(@RequestBody Task task){
        long id = taskService.addTask(task);
        return ResponseResult.okResult(id);
    }

    /**
     * 取消定时任务
     * @param taskId
     * @return
     */
    @DeleteMapping("/{taskId}")
    public ResponseResult cancelTask(@PathVariable("taskId") Long taskId){
        Boolean aBoolean = taskService.cancelTask(taskId);
        return ResponseResult.okResult(aBoolean);
    }

    /**
     * 消费定时任务
     * @return
     */
    @GetMapping("/poll/{taskType}/{priority}")
    public ResponseResult<Task> consumerTask(@PathVariable("taskType") Integer taskType,
                                       @PathVariable("priority") Integer priority){

        Task task = taskService.consumer(taskType, priority);
        return ResponseResult.okResult(task);
    }
}
